<template>
  <div id="app">
    <div class="content">
      <div class="left">
        <a href="/fabu">发作品</a>
      </div>
      <div class="right">
        <div class="top">
          <h3>发作品</h3>
          <span>带有<span class="red">*</span>为必填项</span>
        </div>
        <!-- 文本 -->
        <div class="title">
          <div class="child">
            <span class="red">*</span>
            <span>标题:</span>
          </div>
          <el-input
            type="text"
            placeholder="必填的标题君"
            v-model="text"
            maxlength="25"
            show-word-limit
          >
          </el-input>
        </div>
        <!-- 图片长传 -->
        <div class="images">
          <div class="child">
            <span class="red">*</span>
            <span>封面:</span> 最多上传9张图片
          </div>
          <div class="bottom">
            <el-upload
              action="/ly/fabu/"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :multiple="flag"
              :limit="num"
              :auto-upload="dialogVisible"
              ref="upload"
              :on-change="change"
            >
              <!-- :http-request="submit" -->
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </div>
        </div>
        <!-- 文本域 -->
        <div class="text">
          <div class="child">
            <span></span>
            <span>正文:</span>
          </div>
          <el-input
            type="textarea"
            placeholder="描述下你的作品吧"
            v-model="textarea"
            maxlength="2000"
            show-word-limit
            resize="none"
          >
          </el-input>
        </div>
        <!-- 标签添加 -->
        <div class="biaoqian">
          <div class="child">
            <span></span>
            <span>标签:</span>最多选择4个标签
          </div>
          <div class="btns">
            <el-tag
              :key="tag"
              v-for="tag in dynamicTags"
              closable
              :disable-transitions="false"
              @close="handleClose(tag)"
            >
              {{ tag }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput"
              >添加标签+</el-button
            >
          </div>
          <p>推荐标签</p>
          <div class="tags" @click="add">
            <span>童年</span>
            <span>摸鱼</span>
            <span>作品推荐</span>
            <span>模型</span>
            <span>一周绘画打卡</span>
            <span>约稿</span>
            <span>同人</span>
            <span>原创</span>
            <span>临摹练习</span>
            <span>漫画</span>
            <span>绘画萌新入门创造营</span>
            <span>绘画</span>
          </div>
        </div>
        <!-- 提交 -->

        <div class="foot">
          <!-- <button type="text" @click="open">点击打开 Message Box</button> -->
          <el-button class="faBu" @click="submit" center="true">发布</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: "",
      textarea: "",
      dialogImageUrl: "",
      dialogVisible: false,
      flag: true,
      num: 9,
      dynamicTags: [],
      inputVisible: false,
      inputValue: "",
      imgs: [],
      upImgs: "",
      imageUrl: [],
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除标签
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    //添加标签
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        // if (this.dynamicTags.length <= 4) {
        this.$refs.saveTagInput.$refs.input.focus();
        // }
      });
    },
    //添加标签
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        if (this.dynamicTags.length <= 3) {
          this.dynamicTags.push(inputValue);
        }
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    //添加标签
    add(e) {
      if (e.target.localName == "span") {
        if (this.dynamicTags.length <= 3) {
          this.dynamicTags.push(e.target.innerText);
        }
      }
    },
    change(file, fileList) {
      this.imgs = fileList;
      // this.imgs = fileList[0].raw;
      console.log(fileList);
      // ;
    },
    submit() {
      if (this.$refs.upload.uploadFiles.length == 0) {
        this.$alert("请添加图片", "提示", {
          confirmButtonText: "确定",
        });
      } else if (this.text.length == 0) {
        this.$alert("标题不能为空", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        var formdata = new FormData();
        var text = this.text;
        var textarea = this.textarea;
        var biaoqian = this.dynamicTags;
        formdata.append("title", text);
        for (let i = 0; i < this.imgs.length; i++) {
          formdata.append(`imageUrl${i}`, this.imgs[i].raw);
        }
        this.imageUrl = [];
        if (textarea.length > 0) {
          formdata.append("textarea", textarea);
        }
        if (this.dynamicTags.length > 0) {
          formdata.append("biaoqian", biaoqian);
        }
        this.axios.post("/ly/fabus", formdata).then((res) => {
          this.$alert("发布成功", "提示", {
            confirmButtonText: "确定",
          }).then(() => {
            window.location.href = "/community/zuopin";
          });
        });
      }
    },
  },
};
</script>

<style scoped>
#app {
  background-color: #f6f8fa;
  padding-top: 20px;
  font-size: 14px;
  padding-bottom: 20px;
}
a {
  display: block;
  text-decoration: none;
}
.content {
  width: 1180px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
}
.content a {
  width: 100%;
  color: #525a98;
  border-left: 3px solid #027cdb;
  box-sizing: border-box;
  text-align: center;
}
.left {
  width: 130px;
  background-color: #ffffff;
  margin-right: 20px;
  padding-top: 10px;
  height: 30px;
}
.red {
  color: red !important;
}
.right {
  flex: 1;
  background-color: #fff;
}
.right span {
  font-size: 12px;
  color: #b1bbc3;
}
.right h3 {
  font-weight: 400;
  font-size: 18px;
  padding: 0 30px;
}
.top {
  height: 60px;
  border-bottom: 1px solid #eef3f5;
  display: flex;
  justify-content: flex-start;
  text-align: center;
  line-height: 60px;
}
.child {
  display: inline-block;
}
.child span:nth-child(1) {
  padding-left: 30px;
}

.child span:nth-child(2) {
  font-size: 14px;
  padding-right: 10px;
  color: black;
}
.title {
  margin-top: 30px;
  margin-bottom: 30px;
}
.right /deep/ .el-input {
  width: 600px;
}
.right /deep/.el-input__inner {
  background-color: #f6f8fa;
  border: 1px solid #eef3f5;
  font-size: 12px;
}

.images,
.biaoqian {
  font-size: 10px;
  color: #87959f;
}
/* /deep/ .el-upload {
  padding-left: 30px;
  margin-top: 30px;
} */
.images .bottom {
  padding-top: 30px;
  padding-left: 80px;
}
.right /deep/ .el-textarea {
  width: 600px;
  margin-top: 30px;
  vertical-align: middle;
}
.right /deep/ .el-textarea__inner {
  background-color: #f6f8fa;
  border: 1px solid #eef3f5;
  height: 140px;
  font-size: 16px;
  color: black;
}
.right /deep/ .el-upload {
  background-color: #f6f8fa;
  border: 1px solid #eef3f5;
}
.biaoqian {
  margin-top: 30px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.biaoqian p {
  width: 100%;
  margin: 20px 0;
  margin-left: 77px;
}
.tags {
  width: 700px;
  padding-left: 70px;
  box-sizing: border-box;
}
.biaoqian .tags span {
  display: inline-block;
  padding: 10px;
  background-color: #f6f8fa;
  margin-right: 12px;
  color: #525a67;
  font-size: 12px;
  margin-bottom: 10px;
  border-radius: 5px;
}
.btns {
  padding-top: 20px;
  margin-left: 70px;
  margin-bottom: 30px;
}
.el-button {
  border: 1px solid #027cdb;
  background-color: #fff;
  color: #027cdb;
  font-size: 12px;
  padding: 0 10px;
}
.btns /deep/ .el-tag {
  border: 1px solid #eef3f5;
  color: #87959f;
}
.tags span:hover {
  cursor: pointer;
  background-color: #e3e9ef;
}
.right /deep/.el-input__inner:focus,
.right /deep/.el-textarea__inner:focus {
  border: 1px solid #4d7cfe;
}
.btns .el-input {
  width: 200px;
}
.faBu {
  width: 400px;
  height: 50px;
  background-color: #027cdb;
  border: none;
  color: #fff;
  font-size: 18px;
}
.faBu:hover {
  cursor: pointer;
  background-color: #73b2fc;
}
.foot {
  margin-top: 30px;
  margin-left: 70px;
  margin-bottom: 80px;
}
body /deep/ .el-message-box {
  text-align: center;
  width: 400px;
  height: 300px;
}
.el-message-box__message {
  text-align: center;
}
.foot /deep/ .el-message-box {
  width: 400px;
  height: 300px;
}
</style>